import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutterApp/api/Api.dart';
import '../common/Utils.dart';

class CatchPage extends StatefulWidget {
  CatchPage({Key key}) : super(key: key);

  @override
  _CatchPageState createState() => _CatchPageState();
}

class _CatchPageState extends State<CatchPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  List _alarmList = [];
  List _operateList = [];
  List _recordList = [];

  void _getAlarmList() async {
    //showOkAlertDialog(context:context,title:"登录成功");
    Response response = await alarmList();
    Map<String, dynamic> data = response.data;
    if (!data['isCorrect']) return; 
    setState(() {
      // _alarmList = data['data']['data']['alarmArray'];
    });
  }

  void _getOperateList() async {
    //showOkAlertDialog(context:context,title:"登录成功");
    Response response = await operateList();
    Map<String, dynamic> data = response.data;
    if (!data['isCorrect']) return; 
    setState(() {
      _operateList = data['data']['data']['ctrlArray'];
    });
  }

  void _getRecordList() async {
    //showOkAlertDialog(context:context,title:"登录成功");
    Response response = await recordList();
    Map<String, dynamic> data = response.data;
    if (!data['isCorrect']) return; 
    setState(() {
      _recordList = data['data']['data']['recordArray'];
    });
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    this._getAlarmList();
    this._getOperateList();
    this._getRecordList();
  }

  Widget _createPage(int index) {
    print(index);
    //return Text("异常页面");
    List<Widget> list = new List();
    if (index == 0) {
      list.add(this._getAlarmPage());
    } else if (index == 1) {
      for (int i = 0; i < this._operateList.length; i++) {
        list.add(this._getOperatePage(this._operateList[i]));
      }
    } else {
      for (int i = 0; i < this._recordList.length; i++) {
        list.add(this._getRecordPage(this._recordList[i], i));
      }
    }

    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(bottom: 10),
        child: Column(
          children: list,
        ),
      ),
    );
  }

  Widget _getOperatePage(item) {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
      child: Container(
        decoration: new BoxDecoration(color: Colors.white),
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
              Image.asset(
                'images/rqfm1-1.png',
                width: 30.0,
              ),
              SizedBox(width: 10),
              Text("燃气阀门"),
            ]),
            Divider(),
            Padding(
              padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
              child: Row(children: [
                Expanded(
                    child: Text("操作名称", style: TextStyle(color: Colors.grey))),
                Expanded(
                    child: Text(item['ctrlType'],
                        style: TextStyle(color: Colors.red)))
              ]),
            ),
            Padding(
                padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                child: Row(children: [
                  Expanded(
                      child:
                          Text("控制结果", style: TextStyle(color: Colors.grey))),
                  Expanded(
                      child: Text(item['ctrlFlag'] == '1' ? "控制成功" : "控制失败"))
                ])),
            Padding(
                padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                child: Row(children: [
                  Expanded(
                      child:
                          Text("操作时间", style: TextStyle(color: Colors.grey))),
                  Expanded(child: Text(formateByTime(item['ctrlTime'])))
                ])),
          ],
        ),
      ),
    );
  }

  Widget _getRecordPage(item, index) {
    bool isShowAlarm = index % 2 == 0 ? true : false;
    return Container(
      padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
      child: Container(
        decoration: new BoxDecoration(color: Colors.white),
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
              Image.asset(
                isShowAlarm ? 'images/rqbjq1-1.png' : 'images/rqfm1-1.png',
                width: 30.0,
              ),
              SizedBox(width: 10),
              Text(isShowAlarm ? "燃气报警器" : "燃气阀门"),
            ]),
             Divider(),
            isShowAlarm ? Column(
              children: [
                Padding(
                  padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                  child: Row(children: [
                    Expanded(
                        child:
                            Text("浓度", style: TextStyle(color: Colors.grey))),
                    Expanded(
                        child:
                            Text(item['rqnd1']+'mg/m3', style: TextStyle(color: Colors.red)))
                  ]),
                ),
                Padding(
                    padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                    child: Row(children: [
                      Expanded(
                          child: Text("报警器状态",
                              style: TextStyle(color: Colors.grey))),
                      Expanded(child: Text(item['alarmStatus']))
                    ])),
                Padding(
                    padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                    child: Row(children: [
                      Expanded(
                          child: Text("上报时间",
                              style: TextStyle(color: Colors.grey))),
                      Expanded(child: Text(formateByTime(item['recordTime'])))
                    ])),
              ],
            ): Column(
              children: [
                Padding(
                    padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                    child: Row(children: [
                      Expanded(
                          child: Text("阀门状态",
                              style: TextStyle(color: Colors.grey))),
                      Expanded(child: Text(getValueDes(item['valveStatus']), style: TextStyle(color: Colors.red)))
                    ])),
                Padding(
                    padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                    child: Row(children: [
                      Expanded(
                          child: Text("上报时间",
                              style: TextStyle(color: Colors.grey))),
                      Expanded(child: Text(formateByTime(item['recordTime'])))
                    ])),
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _getAlarmPage() {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
      child: Container(
        decoration: new BoxDecoration(color: Colors.white),
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            IntrinsicHeight(
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      flex: 1,
                      child: Image.asset(
                        'images/ywjc.png',
                        width: 80,
                        height: 80,
                      ),
                    ),
                    Expanded(
                      flex: 2,
                      child: Padding(
                        padding: EdgeInsets.only(left: 15),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(height: 1),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("报警时间",
                                    style: TextStyle(color: Colors.grey)),
                                Container(
                                    child: Text("未处理",
                                        style: TextStyle(color: Colors.red)),
                                    padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                                    decoration: BoxDecoration(
                                        border: Border.all(
                                            color: Colors.red, width: 1)))
                              ],
                            ),
                            Text("2020-03-19 12:20:30"),
                            Text("异常信息", style: TextStyle(color: Colors.red)),
                            SizedBox(height: 1),
                          ],
                        ),
                      ),
                    )
                  ]),
            ),
            Divider(),
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
              Text("开关阀门"),
              Image.asset(
                'images/switch-0.png',
                width: 30.0,
              )
            ])
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: AppBar(
            title: Text("异常页面"),
            centerTitle: true,
            bottom: PreferredSize(
              preferredSize: Size.fromHeight(50),
              child: Material(
                color: Colors.white,
                child: TabBar(
                  labelPadding: EdgeInsets.only(top: 15, bottom: 15),
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicatorColor: Colors.blue,
                  unselectedLabelColor: Colors.black,
                  labelColor: Colors.blue,
                  tabs: <Widget>[
                    Text('报警记录'),
                    Text('操作记录'),
                    Text('上报记录'),
                  ],
                  controller: this._tabController,
                ),
              ),
            )
            // backgroundColor:Color(0xFF000000)
            ),
        // body: this._createPage()
        body: TabBarView(
          controller: this._tabController,
          children: [
            this._createPage(0),
            this._createPage(1),
            this._createPage(2),
          ],
        ),
      ),
    );
  }
}
